[AccessSpecifier] [Modifier] returntype funcName(datatype arg1,...) { Statements ----------- return value; }
class Rectangle { private int length,breadth; public int area() { int a = length*breadth; return a ; } public void setdimension(int m,int n) { length=m; breadth=n; } public void setdimension(int m) { length=m; breadth=m; } } class demo { public static void main(String args[]) { rectangle a = new rectangle(); rectangle b = new rectangle(); rectangle c = new rectangle(); a.setdimension(5,7); b.setdimension(4,6); c.setdimension(8); int z = a.area()+ b.area()+c.area(); System.out.println("Total area is "+ z); } }
Total Area is 123
class Student { private int rollno; private String name; public void showData() { System.out.println("RollNo is "+rollno); System.out.println("Name is "+name); } public void setData(int rn,String nm) { rollno = rn; name = nm; } public void setData(String nm,int rn) { rollno = rn; name = nm ; } } class demo { public static void main(String args[]) { Student a = new Student(); Student b = new Student(); a.setData(4117,"Amit Jain"); b.setData("Seeta Rathi",3012); a.showData(); b.showData(); } }
RollNo is 4117 Name is Amit Jain RollNo is 3012 Name is Seeta Rathi
class Circle { private double R; public void area() { double A=3.14*R*R; System.out.println("Area is "+A); } public void setRadius(int n) { R = n; } public void setRadius(double n) { R = n; } } class demo { public static void main(String args[]) { Circle a = new Circle(); Circle b = new Circle(); a.setRadius(5); b.setRadius(2.5); a.area(); b.area(); } }
Area is 78.5 Area is 19.625